//
//  CTRegisterView.m
//  ConstellationTalk_0.1
//
//  Created by riley on 15/4/20.
//  Copyright (c) 2015年 hanwei. All rights reserved.
//

#import "CTRegisterView.h"
#import "CTHeader.h"
#import "CTDebugManager.h"
#import "UIViewAdditions.h"
#import "CTAppLanguage.h"
#import "CTLoginView_Def.h"

#define CT_REGISTERVIEW_TEXTFIELD_USERNAME_TAG 1000
#define CT_REGISTERVIEW_TEXTFIELD_PASSWORD_TAG 1001
#define CT_REGISTERVIEW_TEXTFIELD_RPASSWORD_TAG 10002

#define CT_REGISTERVIEW_BTN_MAN 2000
#define CT_REGISTERVIEW_BTN_WOMAN 2001

@interface CTRegisterView()<UITextFieldDelegate>
{
    UIImageView *headButton;
    UIView *topView;
    UITextField *userTextField;
    UITextField *passWordTextField;
    UITextField *repeatPWTextField;
    UIImageView *userNameImageView;
    UIImageView *imageView;
    UIButton *womanBtn;
    UIButton *manBtn;
}
@end

@implementation CTRegisterView

@synthesize delegate;
- (void)dealloc
{
    headButton = nil;
    delegate = nil;
    topView = nil;
    userTextField = nil;
    passWordTextField = nil;
    repeatPWTextField = nil;
    userNameImageView = nil;
    imageView  = nil;
    womanBtn = nil;
    manBtn = nil;
    [CTDebugManager showDebugLogInfo:@"dealloc : CTRegisterView Is Removed!!!"];
}

- (instancetype)init
{
    self = [super init];
    if (self)
    {
        self.backgroundColor = CT_HEADER_WHITE_COLOR;
        self.frame = CGRectMake(0, 0,CT_HEADER_SCREEN_WIDTH,CT_HEADER_SCREEN_HEIGHT);
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
        [self addGestureRecognizer:tap];
        
        
        [self createRegisterTitlView];
        [self createRegisterView];
    }
    return self;
}
- (void)createRegisterTitlView
{
    topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CT_HEADER_SCREEN_WIDTH, 64)];
    topView.backgroundColor = CT_HEADER_WHITE_COLOR;
    [self addSubview:topView];
    
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, CT_HEADER_SCREEN_WIDTH, 44)];
    titleLabel.text = [CTAppLanguage localizedStringForKey:@"registerView_titleLabel_text" withValue:nil withTable:@"Language"];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = CT_HEADER_GREEN_COLOR;
    titleLabel.font = [UIFont fontWithName:nil size:19];
    [topView addSubview:titleLabel];
    
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(0, 20, 44, 44);
    [backButton addTarget:self action:@selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [backButton setImage:[UIImage imageNamed:CT_HEADER_BACK_IMAGE] forState:UIControlStateNormal];
    [backButton setImage:[UIImage imageNamed:CT_HEADER_BACK_IMAGE_CLICK] forState:UIControlStateHighlighted];
    [topView addSubview:backButton];
    
    UIButton *registerButton =[UIButton buttonWithType:UIButtonTypeCustom];
    registerButton.frame = CGRectMake(CT_HEADER_SCREEN_WIDTH-44, 20, 44, 44);
    [registerButton addTarget:self action:@selector(registerButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    registerButton.titleLabel.font = [UIFont systemFontOfSize:15.0f];
    [registerButton setTitleColor:CT_HEADER_GREEN_COLOR forState:UIControlStateNormal];
    [registerButton setTitle:[CTAppLanguage localizedStringForKey:@"registerView_registereBtn_title" withValue:nil withTable:@"Language"] forState:UIControlStateNormal];
//    [registerButton setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [topView addSubview:registerButton];
    
    
    UILabel * topBottomLine = [[UILabel alloc]initWithFrame:CGRectMake(0, topView.height-0.5, CT_HEADER_SCREEN_WIDTH, 0.5)];
    topBottomLine.backgroundColor = CT_HEADER_DEEPLINE_COLOR;
    [topView addSubview:topBottomLine];
}
- (void)createRegisterView
{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, topView.bottom, CT_HEADER_SCREEN_WIDTH, CT_HEADER_SCREEN_HEIGHT-topView.height)];
    view.backgroundColor = CT_HEADER_WHITE_COLOR;
    [self addSubview:view];
    
    NSString *repeatPWLabel_Text = [CTAppLanguage localizedStringForKey:@"registerView_rest_passWord_text" withValue:nil withTable:@"Language"] ;
    //头像
    userNameImageView = [[UIImageView alloc]initWithFrame:CGRectMake((view.width-80)/2, 65, 80, 80)];
    userNameImageView.layer.cornerRadius = 40;
    userNameImageView.layer.masksToBounds = YES;
    userNameImageView.userInteractionEnabled = YES;
//    userNameImageView.backgroundColor = [UIColor redColor];
    userNameImageView.image = [UIImage imageNamed:@"head"];
    [view addSubview:userNameImageView];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(headButtonAction:)];
    [userNameImageView addGestureRecognizer:tap];
    
    
    imageView = [[UIImageView alloc]initWithFrame:CGRectMake((view.width-80)/2, 65, 80, 80)];
    imageView.image = [UIImage imageNamed:@"registrationinformation_mengban"];
    [view addSubview:imageView];
    
    headButton = [[UIImageView alloc]init];
    headButton.frame = CGRectMake(imageView.origin.x+23, 94, 34, 22.5);
    headButton.image = [UIImage imageNamed:@"registrationinformation_head_camera"];
    [view addSubview:headButton];

    //用户名
    UIImageView *userImageView = [[UIImageView alloc]initWithFrame:CGRectMake((CT_HEADER_SCREEN_WIDTH-280)/2, userNameImageView.bottom+22, 280, 50)];
    userImageView.image = [UIImage imageNamed:@"resetting_password_shurukuang"];
    [view addSubview:userImageView];
    
    NSString *userLabelText = [CTAppLanguage localizedStringForKey:@"registerView_userName_text" withValue:nil withTable:@"Language"];
    UILabel *userLabel = [[UILabel alloc]initWithFrame:CGRectMake(userImageView.origin.x+10, userImageView.origin.y, userLabelText.length*15, 50)];
    userLabel.font = [UIFont fontWithName:nil size:15];
    userLabel.text = userLabelText;
    userLabel.textColor = LOGINVIEW_USER_HEAD_TEXTFIELD_COLOR;
    userLabel.textAlignment = NSTextAlignmentLeft;
//    [userNameImageView addSubview:userLabel];
    [view addSubview:userLabel];
    
    userTextField = [[UITextField alloc]initWithFrame:CGRectMake(userImageView.origin.x+10+ repeatPWLabel_Text.length*15, userLabel.origin.y, userImageView.width-repeatPWLabel_Text.length*15, 50)];
    userTextField.placeholder = [CTAppLanguage localizedStringForKey:@"registerView_userTextField_text" withValue:nil withTable:@"Language"];
    userTextField.font = [UIFont fontWithName:nil size:15];
    userTextField.textColor = LOGINVIEW_USER_TEXTFIELD_COLOR;
    userTextField.tag = CT_REGISTERVIEW_TEXTFIELD_USERNAME_TAG;
    userTextField.textAlignment = NSTextAlignmentLeft;
//    userTextField.keyboardType = UIKeyboardTypePhonePad;
    userTextField.delegate = self;
    [view addSubview:userTextField];
    
    //性别
    UILabel *sexLabel = [[UILabel alloc]initWithFrame:CGRectMake(userImageView.origin.x+10, userImageView.bottom, repeatPWLabel_Text.length*15, 53)];
    sexLabel.font = [UIFont fontWithName:nil size:15];
    sexLabel.text = [CTAppLanguage localizedStringForKey:@"registerView_sex_label_text" withValue:nil withTable:@"Language"];
    sexLabel.textColor = LOGINVIEW_USER_HEAD_TEXTFIELD_COLOR;
    sexLabel.textAlignment = NSTextAlignmentLeft;
    [view addSubview:sexLabel];
    
    manBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    manBtn.frame = CGRectMake(userTextField.origin.x-30, sexLabel.origin.y, sexLabel.width, sexLabel.height);
    [manBtn addTarget:self action:@selector(sexBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [manBtn setTitle:[CTAppLanguage localizedStringForKey:@"registerView_sex_btn_man" withValue:nil withTable:@"Language"] forState:UIControlStateNormal];
    [manBtn setTitleColor:CT_HEADER_GREEN_COLOR forState:UIControlStateNormal];
    manBtn.tag = CT_REGISTERVIEW_BTN_MAN;
    [view addSubview:manBtn];
    
    womanBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [womanBtn setTitle:[CTAppLanguage localizedStringForKey:@"registerView_sex_btn_woman" withValue:nil withTable:@"Language"] forState:UIControlStateNormal];
    womanBtn.tag = CT_REGISTERVIEW_BTN_WOMAN;
    [womanBtn addTarget:self action:@selector(sexBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    womanBtn.frame = CGRectMake(manBtn.right, sexLabel.origin.y, sexLabel.width, sexLabel.height);
    [womanBtn setTitleColor:LOGINVIEW_REGISTER_SEX_COLOR forState:UIControlStateNormal];
    [view addSubview:womanBtn];
    
    //密码
    UIImageView *passWordImageView = [[UIImageView alloc]initWithFrame:CGRectMake(userImageView.origin.x, sexLabel.bottom, userImageView.width, 50)];
    passWordImageView.image = [UIImage imageNamed:@"resetting_password_shurukuang"];
    [view addSubview:passWordImageView];
    
    NSString *passWordLabel_text = [CTAppLanguage localizedStringForKey:@"registerView_passWord_text" withValue:nil withTable:@"Language"];
    UILabel *passWordLabel = [[UILabel alloc]initWithFrame:CGRectMake(passWordImageView.origin.x+10, passWordImageView.origin.y, passWordLabel_text.length*15, passWordImageView.height)];
    passWordLabel.font = [UIFont fontWithName:nil size:15];
    passWordLabel.text = [CTAppLanguage localizedStringForKey:@"registerView_passWord_text" withValue:nil withTable:@"Language"];
    passWordLabel.textColor = LOGINVIEW_USER_HEAD_TEXTFIELD_COLOR;
    passWordLabel.textAlignment = NSTextAlignmentLeft;
    [view addSubview:passWordLabel];
    
    passWordTextField = [[UITextField alloc]initWithFrame:CGRectMake(passWordImageView.origin.x+10+ repeatPWLabel_Text.length*15, passWordImageView.origin.y, passWordImageView.width-repeatPWLabel_Text.length*15, 50)];
    passWordTextField.font = [UIFont fontWithName:nil size:15];
    passWordTextField.textColor = LOGINVIEW_USER_TEXTFIELD_COLOR;
    passWordTextField.placeholder = [CTAppLanguage localizedStringForKey:@"registerView_passWordTextField_text" withValue:nil withTable:@"Language"];
    passWordTextField.keyboardType = UIKeyboardTypeDefault;
    passWordTextField.secureTextEntry = YES;
    passWordTextField.tag = CT_REGISTERVIEW_TEXTFIELD_PASSWORD_TAG;
    passWordTextField.textAlignment = NSTextAlignmentLeft;
    passWordTextField.delegate = self;
    [view addSubview:passWordTextField];
    
    //重新输入密码
    UIImageView *repeatPWImageView = [[UIImageView alloc]initWithFrame:CGRectMake(passWordImageView.origin.x, passWordImageView.bottom+15, passWordImageView.width, passWordImageView.height)];
    repeatPWImageView.image = [UIImage imageNamed:@"resetting_password_shurukuang"];
    [view addSubview:repeatPWImageView];
    

    UILabel *repeatPWLabel = [[UILabel alloc]initWithFrame:CGRectMake(repeatPWImageView.origin.x+10, repeatPWImageView.origin.y, 15*repeatPWLabel_Text.length, repeatPWImageView.height)];
    repeatPWLabel.font = [UIFont fontWithName:nil size:15];
    repeatPWLabel.text = repeatPWLabel_Text;
    repeatPWLabel.textColor = LOGINVIEW_USER_HEAD_TEXTFIELD_COLOR;
    repeatPWLabel.textAlignment = NSTextAlignmentLeft;
    [view addSubview:repeatPWLabel];
    
    repeatPWTextField = [[UITextField alloc]initWithFrame:CGRectMake(repeatPWLabel.right, repeatPWImageView.origin.y, repeatPWImageView.width-repeatPWLabel.width, 50)];
    repeatPWTextField.font = [UIFont fontWithName:nil size:15];
    repeatPWTextField.textColor = LOGINVIEW_USER_TEXTFIELD_COLOR;
    repeatPWTextField.tag = CT_REGISTERVIEW_TEXTFIELD_RPASSWORD_TAG;
    repeatPWTextField.textAlignment = NSTextAlignmentLeft;
    repeatPWTextField.secureTextEntry = YES;
    repeatPWTextField.keyboardType = UIKeyboardTypeDefault;
    repeatPWTextField.placeholder = [CTAppLanguage localizedStringForKey:@"registerView_rest_passWordTextField_text" withValue:nil withTable:@"Language"];
    repeatPWTextField.delegate = self;
    [view addSubview:repeatPWTextField];
    
}
//注册
- (void)registerButtonAction:(UIButton *) sender
{
    [userTextField resignFirstResponder];
    [passWordTextField resignFirstResponder];
    [repeatPWTextField resignFirstResponder];
    if ([self.delegate respondsToSelector:@selector(registerViewRegisterButton:)])
    {
        [self.delegate registerViewRegisterButton:self];
    }
    
}
//设置性别
- (void)sexBtnAction:(UIButton *) sender
{

    if ([self.delegate respondsToSelector:@selector(registerViewsexBtnText:registerView:)])
    {
        [self.delegate registerViewsexBtnText:sender.titleLabel.text registerView:self];
    }
    
    switch (sender.tag) {
        case CT_REGISTERVIEW_BTN_MAN:
            [manBtn setTitleColor:CT_HEADER_GREEN_COLOR forState:UIControlStateNormal];
            [womanBtn setTitleColor:LOGINVIEW_REGISTER_SEX_COLOR forState:UIControlStateNormal];
            break;
        case CT_REGISTERVIEW_BTN_WOMAN:
            [womanBtn setTitleColor:CT_HEADER_GREEN_COLOR forState:UIControlStateNormal];
            [manBtn setTitleColor:LOGINVIEW_REGISTER_SEX_COLOR forState:UIControlStateNormal];
            break;
            
        default:
            break;
    }
}
//返回按钮
- (void)backButtonAction:(UIButton *) sender
{
    if ([self.delegate respondsToSelector:@selector(registerViewBackButton:)])
    {
        [self.delegate registerViewBackButton:self];
    }
}
//头像
- (void)headButtonAction:(UITapGestureRecognizer *) sender
{
    if ([self.delegate respondsToSelector:@selector(registerViewHeadButton:)])
    {
        [self.delegate registerViewHeadButton:self];
    }
}
-(void)setImage:(UIImage *)image
{
    [imageView removeFromSuperview];
    [headButton removeFromSuperview];
    userNameImageView.image = image;
}
#pragma markd TextFieldDelegate
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    
}// became first responder

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    
    switch (textField.tag) {
        case CT_REGISTERVIEW_TEXTFIELD_USERNAME_TAG:
            if ([self.delegate respondsToSelector:@selector(registerViewUserTextFieldText:registerView:)])
                [self.delegate registerViewUserTextFieldText:textField.text registerView:self];
            break;
        case CT_REGISTERVIEW_TEXTFIELD_PASSWORD_TAG:
            if ([self.delegate respondsToSelector:@selector(registerViewPassTextFieldText:registerView:)])
                [self.delegate registerViewPassTextFieldText:textField.text registerView:self];
            break;
        case CT_REGISTERVIEW_TEXTFIELD_RPASSWORD_TAG:
            if ([self.delegate respondsToSelector:@selector(registerViewPassWordTextFieldText:registerView:)])
                [self.delegate registerViewPassWordTextFieldText:textField.text registerView:self];
            break;
        default:
            break;
    }
}
- (void)tapAction:(UITapGestureRecognizer *)tap
{
    [userTextField resignFirstResponder];
    [passWordTextField resignFirstResponder];
    [repeatPWTextField resignFirstResponder];
}
@end
